<?php

/* * ***********************************************************************
  Class Validate data input
  ----------------------------------------------------------------
  PICKCO CMS Project
  Company: PICKCO  Co., Ltd
  Email: admin@pick.vn
  Last updated: 05/08/2008
 * ************************************************************************ */

class Pick_Data_Validate {

    var $messages;

    function Pick_Data_Validate() {
        global $messages;
        $this->messages = $messages;
    }

    function validString($name, $str) {
        if (!$str)
            return $name . " " . l('notValidString');
        return '';
    }

    function validArray($name, $array) {
        if (!is_array($array))
            return $name . " " . l('notselected');
        return '';
    }

    function validStatus($name, $str) {
        if ($str == -1)
            return $name . " " . l('notselected');
        return '';
    }

    function validPassword($name, $str) {
        if (strlen($str) < 6)
            return $name;
        return '';
    }

    function validTestPass($name, $password, $confirm) {
        if ($password === $confirm)
            return "";
        return $name;
    }

    function validEmail($name, $email) {
        $lengthPattern = "/^[^@]{1,64}@[^@]{1,255}$/";
        $syntaxPattern = "/^((([\w\+\-]+)(\.[\w\+\-]+)*)|(\"[^(\\|\")]{0,62}\"))@(([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9]{2,})|\[?([1]?\d{1,2}|2[0-4]{1}\d{1}|25[0-5]{1})(\.([1]?\d{1,2}|2[0-4]{1}\d{1}|25[0-5]{1})){3}\]?)$/";
        return ((preg_match($lengthPattern, $email) > 0) && (preg_match($syntaxPattern, $email) > 0)) ? '' : $name . " " . $this->messages['notValidEmail'];
    }

    function validTestEmail($name, $email, $reemail) {
        if ($email != $reemail)
            return $name . "  " . $this->messages['notValidTestEmail'];
        return '';
    }

    function validUrl($name, $url) {
        $error = '';
        $domain = "(http(s?):\/\/|ftp:\/\/)*([[:alpha:]][-[:alnum:]]*[[:alnum:]])(\.[[:alpha:]][-[:alnum:]]*[[:alpha:]])+";
        $dir = "(/[[:alpha:]][-[:alnum:]]*[[:alnum:]])*";
        $trailingslash = "(\/?)";
        $page = "(/[[:alpha:]][-[:alnum:]]*\.[[:alpha:]]{3,5})?";
        $getstring = "(\?([[:alnum:]][-_%[:alnum:]]*=[-_%[:alnum:]]+)(&([[:alnum:]][-_%[:alnum:]]*=[-_%[:alnum:]]+))*)?";
        $pattern = "^" . $domain . $dir . $trailingslash . $page . $getstring . "$";
        $check = eregi($pattern, $url);
        if (!$check)
            $error = $name . "  " . l('notValidUrl');
        return $error;
    }

    function validInteger($name, $value) {
        if ($value == '')
            return $name . " - " . $this->messages['notValidInteger'];
        if (!is_numeric($value))
            return $name . " - " . $this->messages['notValidInteger'];
        if (!is_int((int) $value))
            return $name . " - " . $this->messages['notValidInteger'];
        return '';
    }

    function validDateTime($name, $dateTime) {
        if (preg_match("/^(\d{4})-(\d{2})-(\d{2}) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/", $dateTime, $matches)) {
            if (checkdate($matches[2], $matches[3], $matches[1])) {
                return '';
            }
        }
        return $name . " - " . l('notValidDateTime');
    }

    function validUsername($name, $username) {
        $error = '';
        if (preg_match("/^([a-z0-9_]+)$/", $username)) {
            return '';
        }
        return $name;
    }

    function validSubdomain($name, $subdomain) {
        $error = '';
        if (!$subdomain)
            return $name . " - " . l('notValidSubdomain');
        if (preg_match("/^([a-z0-9_]+)$/", $subdomain)) {
            return '';
        }
        return $name . " - " . l('notValidSubdomain');
    }

    function validFileName($name, $fileName) {
        $error = '';
        if (!$fileName)
            return $name . " - " . l('notValidFileName');
        if (preg_match("/^([a-z0-9_]+)$/", $fileName)) {
            return '';
        }
        return $name . " - " . l('notValidFileName');
    }

    function validSlug($name, $slug) {
        $error = '';
        if (!$slug)
            return $name . " - " . l('notValidSlug');
        if (preg_match("/^([a-z0-9_-]+)$/", $slug)) {
            return '';
        }
        return $name . " - " . l('notValidSlug');
    }

    function validCheckType($name, $str) {
        if (!$str)
            return $name . " - " . l('check_type');
        return '';
    }

    function validPermissions($name, $array) {
        if ($array)
            return '';
        else
            return $name;
    }

    function validImage($str) {
        $img = substr($str, -3);
        if (preg_match("/jpg|png|bmp|gif/", $img))
            return 1;
        return 0;
    }

    function validVideo($str) {
        $ext = strtolower(substr($str, -3));
        if (preg_match("/avi|flv|swf/", $ext))
            return 1;
        return 0;
    }

    function validCaptchar($name, $str) {
        if ($_SESSION && isset($_SESSION['securimage_code_value']['default'])) {
            $default = $_SESSION['securimage_code_value']['default'];
            if (strtolower($str) == strtolower($default)) {
                return "";
            }
        }
        return $name;
    }

}